package com.ruoyi.web.controller.article;



import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.weixin.anno.RequireLogin;
import com.ruoyi.weixin.entity.*;
import com.ruoyi.weixin.service.SgArticleService;
import com.ruoyi.weixin.service.SgCategoryService;
import com.ruoyi.weixin.service.SgLinkService;
import com.ruoyi.weixin.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章表(SgArticle)表控制层
 *
 * @author makejava
 * @since 2024-01-22 13:46:20
 */

@RestController
@RequestMapping("/wexinLogin/sgArticle")
public class SgArticleController  {
    /**
     * 服务对象
     */

    @Resource
    private SgCategoryService sgCategoryService;
    @Resource
    private SgArticleService sgArticleService;

    @Resource
    UserService userService;

    @DataSource(value = DataSourceType.SLAVE)
    @PostMapping("hot")
    public R selectAll(@RequestBody SgArticle sgArticle) {
        Page<SgArticle> page = new Page<SgArticle>(sgArticle.getPageNum(),sgArticle.getPageSize());
        LambdaQueryWrapper<SgArticle> eq = new LambdaQueryWrapper<SgArticle>().eq(SgArticle::getIsTop, 1).eq(SgArticle::getStatus, 0).eq(SgArticle::getDelFlag, 0);
        Page<SgArticle> page1 = sgArticleService.page(page, eq);
        return R.ok(page1);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @PostMapping("listALL")
    public R listALL(@RequestBody SgArticle sgArticle) {
        Page<SgArticle> page = new Page<SgArticle>(sgArticle.getPageNum(),sgArticle.getPageSize());
        LambdaQueryWrapper<SgArticle> eq = new LambdaQueryWrapper<SgArticle>().eq(SgArticle::getStatus, 0).eq(SgArticle::getDelFlag, 0).orderByDesc(SgArticle::getIsTop);
        if (sgArticle.getCategoryId()!=null&& sgArticle.getCategoryId()!=0){
            eq.eq(SgArticle::getCategoryId, sgArticle.getCategoryId());
        }
        if (sgArticle.getTitle()!=null){
            eq.like(SgArticle::getTitle,sgArticle.getTitle());
        }
        Page<SgArticle> page1 = sgArticleService.page(page, eq);
        return R.ok(page1);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @PostMapping("airtleById")
    public R airtleById(Long id) {
        SgArticle one = sgArticleService.getByIdd(id);
        return R.ok(one);
    }

    @PostMapping("userList")
    public R userList(@RequestBody PagesResult pagesResult) {
        Page<User> userPage = new Page<>(pagesResult.getPageNum(),pagesResult.getPageSize());
        ArrayList<String> list = new ArrayList<>();
        list.add("1");list.add("31");
        Page<User> page = userService.page(userPage,new LambdaQueryWrapper<User>().notIn(User::getFrentLink,list));
        return R.ok(page);
    }

    @PostMapping("userObj")
    public R userObj(String id) {
        User byId = userService.getById(id);
        return R.ok(byId);
    }


    @PostMapping("edi")
    public R userObj(@RequestBody User user) {
        return R.ok(userService.updateById(user));
    }
}

